package com.library.service.student;

import com.library.dao.StudentDao;
import com.library.entity.Student;
import javax.servlet.http.HttpServletRequest;

public class StudentService {
    private StudentDao studentDao = new StudentDao();

    // 登录（带HttpServletRequest参数，便于设置错误信息）
    public boolean login(String sno, String password, javax.servlet.http.HttpServletRequest request) {
        if (sno == null || sno.trim().isEmpty() || password == null || password.trim().isEmpty()) {
            request.setAttribute("errorMsg", "学号或密码不能为空！");
            return false;
        }
        Student student = studentDao.getBySno(sno);
        if (student == null || !student.getPassword().equals(password)) {
            request.setAttribute("errorMsg", "学号或密码错误！");
            return false;
        }
        // 登录成功，写入session
        request.getSession().setAttribute("student", student);
        return true;
    }

    // 注册
    public String register(Student student) {
        if (studentDao.getBySno(student.getSno()) != null) {
            return "学号已存在";
        }
        studentDao.insert(student);
        return "注册成功";
    }
}